<?php
//require './includes/common.inc.php';
//if(!$M['allowregister']) showmessage('该网站不能注册，请与网站管理员联系');
//if($SETTING['enableserverpassport'])
//{
//	$registerurl = $SETTING['passport_serverurl'].$SETTING['passport_registerurl'];
//	if(QUERY_STRING) $registerurl .= strpos($registerurl, '?') ? '&'.QUERY_STRING : '?'.QUERY_STRING;
//	header('location:'.$registerurl);
//	exit;
//}

require_once 'member.lang.php';
require_once 'member_admin.lang.php';
require_once 'member.func.php';

//if(!isset($action)) $action = 'register';

/*if($action != 'login'){
	if(!$_userid) showmessage($LANG['please_login'], "?module=$module&action=login&forward=".urlencode(URL));
}*/

if(!$forward) $forward = HTTP_REFERER;

// 加载表单类
include WWW_ROOT.'includes/form.class.php';

// 加载会员表单类
if(!class_exists('member_form'))
{
	require CACHE_MODEL_PATH.'member_form.class.php';
}
$member_form = new member_form($_modelid);

// 加载会员表单输入类
if(!class_exists('member_input'))
{
	require CACHE_MODEL_PATH.'member_input.class.php';
}
$member_input = new member_input($_modelid);


// 加载会员类
//$member = load('member.class.php', 'member');
include_once 'includes/member.class.php';
$member = new member();

// 加载获取会员头像
$avatar = avatar($_userid);

// 加载会员组类
require 'includes/group.class.php';
$group = new group();

if(!$action) header('Location:'.url("?module=member&action=edit"));
switch ($action)
{
	// AJAX登录
	case 'login_ajax':
		
		if($_userid) showmessage($LANG['you_have_logined'], url("?module=$module"));
		/*$check = new times();
		$check->set('checkcode', 3600, 1);*/

		$account = iconv('utf-8', CHARSET, $account);
		$password = iconv('utf-8', CHARSET, $password);

		$info = $member->login($account, $password, $cookietime);
		if(!$info)
		{
            //$check->add();
			echo '0';
			exit;
		}
		else
		{
			echo 1;
			exit;
		}
	break;

	// 用户登录
    case 'login':
	
		//if($_userid) showmessage($LANG['you_have_logined'], url("?module=$module"));
		if($_userid) header("Location:".url("?module=$module"));
		
		/*$check = new times();
		$check->set('checkcode', 3600, 1);*/
		/*if($SETTING['enableserverpassport'])
		{
			$loginurl = $SETTING['passport_serverurl'].$SETTING['passport_loginurl'];
			if(QUERY_STRING) $loginurl .= strpos($loginurl, '?') ? '&'.QUERY_STRING : '?'.QUERY_STRING;
			elseif($username && $password && $dosubmit) $loginurl .= "?username=$username&password=$password&cookietime=$cookietime&dosubmit=1";
			header('location:'.$loginurl);
			exit;
		}*/

		if($dosubmit)
		{
			//if($check->check()) checkcode($checkcodestr, 1, HTTP_REFERER);
			/*if($SETTING['uc'])
			{
				$action = 'login';
				require MOD_ROOT.'api/passport_server_ucenter.php';
				$member->edit_password_username($username, $password);
			}*/
			$cookietime = $remember ? 2592000 : 0; // 保存一月
		    $info = $member->login($account, $password, $cookietime);
		    if(!$info)
			{
				//$check->add();
				showmessage($member->msg(), HTTP_REFERER);
			}
			else
			{
				//$check->clear();
				//$forward = isset($forward) ? url($forward, 1) : SITE_URL;
				/*if($SETTING['enablepassport'])
	            {
			        $action = 'login';
					if($SETTING['passport_charset'] && $SETTING['passport_charset'] != CHARSET)
					{
						$info = str_charset(CHARSET, $SETTING['passport_charset'], $info);
					}
					$info['password'] = md5($password);
					extract($info);
					require MOD_ROOT.'api/passport_server_'.$SETTING['passport_file'].'.php';
					header('location:'.$url);
                    exit;
				}*/
				
				
				
				// 设置js用的cookie，如果不用应该可以屏蔽掉20121030
				/*if($cookietime) $jscookiedays = $cookietime/3600/24;
				$script = "<script language='javascript'>";
				$script .= "setcookie('username', '".$username."', '".$jscookiedays."');";
				$script .= "</script>";*/
				
				$forward = $forward && strpos($forward, 'logout') == false ? $forward : SITE_URL/*url('?module=member')*/;
				showmessage($LANG['login_success'].$code.$script,$forward);
			}
		}
		else
		{
			/*$select = array();
			$cookietime = intval(get_cookie('cookietime'));
			$cookietimes = array(0, 86400, 2592000, 31536000);
			foreach($cookietimes as $v)
			{
				$select[$v] = $v == $cookietime ? 'selected' : '';
			}*/
			$cok_username = get_cookie('username');
			$cok_auth = get_cookie('auth');
			include template('login.html','member');
		}
	break;
	
	// 选择模型
	case 'choice_model':
		if(!$M['allowregister']) showmessage('该网站不能注册，请与网站管理员联系');
		if(!$forward) $forward = HTTP_REFERER;
		if($_userid) showmessage($LANG['you_have_logined'], $forward);
		if(!$M['choosemodel'] || $member->count_model() < 2) showmessage('请添加用户模型', $forward);
		if($dosubmit)
		{
			$modelname = $member->MODEL[$modelid]['name'];
			showmessage("已选择$modelname,请填写注册信息", url("?module=member&action=register&modelid=$modelid"));
		}
		else
		{
			$model = $member->MODEL;
			include template('choice_model.html','member');
		}
	break;
	
	case 'register_model':
		if(!$M['allowregister']) showmessage('该网站不能注册，请与网站管理员联系');
		if(!$_userid) showmessage($LANG['please_login'], url("?module=member&action=login&forward=".urlencode(URL)));
		
		if(!class_exists('member_form'))
		{
			require CACHE_MODEL_PATH.'member_form.class.php';
		}
		$member_form = new member_form($_modelid);
		if(count($member_form->fields) < 1) 
		{
			header('Location:'.SITE_URL);
			exit;
		}
		
		if(!class_exists('member_input'))
		{
			require CACHE_MODEL_PATH.'member_input.class.php';
		}
		$member_input = new member_input($_modelid);
		
		if(!class_exists('member_update'))
		{
			require CACHE_MODEL_PATH.'member_update.class.php';
		}
		$member_update = new member_update($_modelid, $_userid);
		
		if(!class_exists('model_member_field'))
		{
			MOD_ROOT.'member/model_member_field.class.php';
		}
		if($dosubmit)
		{
			$inputinfo = $member_input->get($info);
			$modelinfo = $inputinfo['model'];
			$modelinfo['userid'] = $_userid;
			$member_update->update($modelinfo);
			$member->edit_model($_modelid, $modelinfo);
			foreach($MODEL AS $modelid=>$value)
			{
				if($modelid == $_modelid && $value['modeltype']==2 && $value['tablename'] == 'company')
				{
					$yp_setting = cache_read('module_yp.php');
					showmessage('注册成功，即将进入商务中心', SITE_URL.'yp/'.$yp_setting['businessdir'].'/');
				}
			}
			showmessage($LANG['operation_success'], SITE_URL);
		}
		else
		{
			$data = $member->get_model_info($_userid, $_modelid);
			$forminfos = $member_form->get($data);
			if(!$forminfos) 
			{
				header('location:'.url(SITE_URL, 1));
			}
			$head['title'] = '填写详细资料_新用户注册_'.$SETTING['sitename'];
			include template('register_model.html','member');
		}
	break;
	
	/*进行用户注册*/
	case 'register':
		if($dosubmit)
		{
			
			//对不起，本站不允许新用户注册！
			//checkcode($checkcodestr, $M['enablecheckcodeofreg'], $forward);
			/*if($SETTING['uc'])
			{
				extract($memberinfo);
				require MOD_ROOT.'api/passport_server_ucenter.php';
			}*/
			
			if(!$userid)
			{
				$userid = $member->register($memberinfo);
			}
			if(!$userid) showmessage($member->msg(), $forward);
			
			/*
				新会员注册需要邮件验证
			*/
			/*if($M['enablemailcheck'])
			{*/
				if(!class_exists('sendmail'))
				{
					$sendmail = load('sendmail.class.php');
				}
				$authstr = $member->make_authcode($memberinfo);
				$title = $SETTING['sitename'].$LANG['member_register_email_verify'];
				$authurl = url("?module=$module&action=activate&userid=$userid&authcode=$authstr");
				$content = tpl_data($module, 'register_mailcheck.html');
				$sendmail->send($memberinfo['email'], $title, stripslashes($content), $SETTING['mail_user']);
			//}

			/* 自动登录并设置一个7天过期的COOKIE */
			set_cookie('auth', site_auth($userid."\t".$member->password($memberinfo['password']), 'ENCODE', md5(AUTH_KEY.$_SERVER['HTTP_USER_AGENT'])), time()+3600*24*7);
			showmessage('注册成功！正在自动为您登录'.$script, url("?module=member&action=login"));
		
			
			if($SETTING['enablepassport'])
			{
				if($SETTING['passport_charset'] && $SETTING['passport_charset'] != CHARSET)
				{
					$info = str_charset(CHARSET, $SETTING['passport_charset'], $memberinfo);
				}
				
				extract($memberinfo);
				$password = md5($memberinfo['password']);
				require MOD_ROOT.'api/passport_server_'.$SETTING['passport_file'].'.php';
				header('location:'.$url);
			}
			/*if($memberinfo['modelid'] && $M['choosemodel'] && !$M['enablemailcheck'] && !$M['enableadmincheck'])
			{
				$result = $member->login($memberinfo['username'], $memberinfo['password']);
				showmessage('开始填写详细资料！'.$script, '?module=member&action=register_model');
			}*/
			if($member->msg())
			{
				showmessage($member->msg().$script, url("?module=member&action=login"));
			}
			else
			{
				showmessage('注册成功！'.$script, url("?module=member&action=login"));
			}
		}
		else
		{	
			//当用户以经登录存在$_userid时不得重复登录
			//if($_userid && $action != 'activate') showmessage("不允许重复登录！",url('?module=member'));	
			if($_userid && $action != 'activate') header("Location:".url("?module=$module"));
				
			if(!$forward) $forward = HTTP_REFERER;

			/*if($M['choosemodel'] && $member->count_model() >= 2 && !$modelid)
			{
				//header('Location: '.url("?module=member&action=choice_model"));
				header('Location: '.url("?module=member&action=register&modelid=10"));
				exit;
			}*/
			//if($modelid && !isset($member->MODEL[$modelid])) showmessage('该模型不存在');
			$head['title'] = '新用户注册_'.$SETTING['sitename'];
			include template('register.html','member');
		}
	break;
	
	case 'checkcredit':
		$outcredit = cache_read('creditsettings.php');
		if(!$outcredit && $_groupid==1) showmessage('没有相关的更新积分兑换方案，请到Ucenter处添加', UC_API);
		$avatar = avatar($_userid);
		
		$pay_api = load('pay_api.class.php', 'pay');
		$arr_credit = array(1=>'point', 2=>'amount');
		$credit = $arr_credit[$fromcreditid];
		
		$orgcredit = $member->get($_userid, $credit);
		if($orgcredit[$credit] < $value)
		{
			exit('超过'.$credit.'的最大值');
		}
		else
		{
			exit('success');
		}
		break;
	
	// 兑换积分
	case 'changecredit':
		$outcredit = cache_read('creditsettings.php');
		//if(!$outcredit && $_groupid==1) showmessage('没有相关的更新积分兑换方案，请到Ucenter处添加', UC_API);
		$avatar = avatar($_userid);
		
		$pay_api = load('pay_api.class.php', 'pay');
		$arr_credit = array(1=>'point', 2=>'amount');
		$credit = $arr_credit[$fromcreditid];
		
		if($dosubmit)
		{
			if(!$member->verfy_password($_userid, $password))
			{
				showmessage('密码不正确', $forward);
			}
			$orgcredit = $member->get($_userid, $credit.',`touserid`');
			if($orgcredit[$credit] < $fromcredit)
			{
				showmessage('超过你所有的最大值', $forward);
			}
			if($orgcredit['touserid']<=0)
			{
				showmessage('用户整合出错,请检查', $forward);
			}
			$inputCredit = $fromcredit;
			$key = $toappid.'|'.$tocreditid;
			$fromcredit = floor($fromcredit / $outcredit[$key]['ratio']);
			$ucresult = uc_call('uc_credit_exchange_request', array($orgcredit['touserid'], $fromcreditid, $tocreditid, $toappid, $fromcredit));
			if(!$ucresult)
			{
				showmessage('API有误，请检查', $forward);
			}
			$pay_api->update_exchange($module, $arr_credit[$fromcreditid], -$inputCredit, '兑换积分', $_userid);
			showmessage('操作成功', $forward);
		}
		else
		{
			$arr_select[0] = '请选择';
			foreach($outcredit as $keyid=>$item)
			{
				$arr_select[$keyid] = $item['title'];
			}
			$siteCreditSrcOption=array(
				'1'=>'点数',
				'2'=>'金钱'
			);
			$creditsrcOption=array();
			foreach($outcredit as $item)
			{
				$creditsrcOption[$item['creditsrc']] = $siteCreditSrcOption[$item['creditsrc']];
			}
			include template('chancredit.html','member');
		}
	break;
	
	/*验证邮件注册登录*/
	case 'activate':
			$result = $member->verify_authcode($userid, $authcode);
			if(!$result) showmessage($member->msg(), HTTP_REFERER);
			showmessage($LANG['mail_verify_success'], url("?module=$module&action=login"));
		break;
	/*进行用户名检查,用于注册时做用户体验*/
	case 'checkuser':
			if(!$member->is_username($value) || $member->username_exists($value))
			{
				exit($member->msg());
			}
			else
			{
				if($M['preserve'])
				{
					$PRES = explode(',', $M['preserve']);
					if(in_array($value, $PRES))
					{
						exit('此用户名受保护，不允许注册！');
					}
				}
				exit('success');
			}
		break;
		/*进行昵称检查,用于注册时做用户体验*/
	case 'checknickname':
			if($db->get_one("SELECT userid FROM $member->table WHERE nickname='$value' AND userid!='$_userid'"))
			{
				exit('昵称已经被人使用了！');
			}
			else
			{
				exit('success');
			}
		break;
	/*进行手机号检查，用于注册时做用户体验*/
	case 'checkmobile':
			if(!is_mobile($value))
			{
				exit('手机号格式不正确！');
			}
			elseif($db->get_one("SELECT userid FROM $member->table WHERE mobile='$value' AND userid!='$_userid'"))
			{
				exit('手机号已经被人使用了！');
			}
			else
			{
				exit('success');
			}
		break;
	/*进行邮件检查，用于注册时做用户体验*/
	case 'checkemail':
			if(!is_email($value))
			{
				exit($LANG['input_valid_email']);
			}
			elseif(/*!$M['allowemailduplicate'] && */$member->email_exists($value,$_userid))
			{
				exit($member->msg());
			}
			else
			{
				exit('success');
			}
		break;
		case 'regagreement':
			//exit($M['reglicense']);
			include template('regagreement.html','member');
		break;
	
	// 用户头像修改
	case 'avatar':
	
		if(!$_userid) showmessage('您还没有登陆，请登陆',url("?module=member&action=login&forward=".urlencode(URL)));
		if($dosubmit)
		{
			 if(!is_object($attachment))
			{
				include MOD_ROOT.'attachment/attachment.class.php';
				$attachment = new attachment($module);
			}
			$aids = $attachment->upload('preview', 'jpg|jpeg|gif|bmp|png', UPLOAD_MAXSIZE, 1);
			if(!$aids) showmessage($attachment->error());
			
			$member->edit(array('avatar'=>$aids[0]));
			
			// 输出头像
			//$avatar = avatar($_userid);
			//echo $avatar;
			showmessage("修改头像成功","?module=$module");
		}
		else
		{
			$avatar = avatar($_userid);
			include template('avatar.html','member');
		}
	break;
	
	
	// 会员升级
    case 'upgrade' :
		if(!$_userid) showmessage('您还没有登陆，请登陆',url("?module=member&action=login&forward=".urlencode(URL)));
		$data = array();
	    $today = date('Y-m-d');
		$groups = $group->extend_list($_userid);
		$array = $group->listinfo('`issystem`=0 AND disabled=0', '`listorder`');
		foreach($array as $k=>$v)
		{
			$groupid = $v['groupid'];
			$v['iscontinue'] = isset($groups[$groupid]) ? 1 : 0;
			$v['isexpired'] = $groups[$groupid]['enddate'] < $today ? 1 : 0;
			if($v['iscontinue'])
			{
				$v['startdate'] = $groups[$groupid]['startdate'];
				$v['enddate'] = $groups[$groupid]['enddate'];
			}
			$data[$groupid] = $v;
		}

		$head['title'] = '会员升级_会员中心_'.$SETTING['sitename'];
	    include template('upgrade.html','member');
		break;
		
	case 'upgrade_view':
		$r = $group->get($groupid);
	    if(!$r) showmessage('会员组不存在！');
		extract($r);
		$head['title'] = '查看会员服务_会员中心_'.$SETTING['sitename'];
		include template('upgrade_view.html','member');
		break;

    case 'upgrade_pay':
		$r = $group->get($groupid);
	    if(!$r) showmessage('会员组不存在！');
		extract($r);
	
        if($disabled || !$allowupgrade) showmessage('系统已禁止自助升级至此会员组！');

		if($dosubmit)
	    {
			if(!in_array($unit, array('y', 'm', 'd')) || !preg_match("/^[0-9]+$/", $number) || $number < 1) showmessage('参数错误！');
			$price = $r['price_'.$unit];
			if(intval($price) == 0) showmessage('价格为0，不允许升级！');
			$total = $number*$price;
			$pay_api = load('pay_api.class.php', 'pay');
			if(!$pay_api->update_exchange('member', $M['paytype'], -$total, "会员升级"))
			{
				showmessage($pay_api->error());
			}
			$group->extend_upgrade($_userid, $groupid, $unit, $number, date('Y-m-d'));
			showmessage('操作成功！', $forward);
		}
		else
	    {
			$balance = $M['paytype'] == 'amount' ? $_amount : $_point;
		    $head['title'] = '会员升级_会员中心_'.$SETTING['sitename'];
			include template('upgrade_pay.html','member');
		}
		break;

    case 'upgrade_continue':
		$r = $group->extend_get($_userid, $groupid);
	    if(!$r) showmessage('会员组不存在！');
		extract($r);
        if($disabled || !$allowupgrade) showmessage('系统已禁止自助续费此会员组！');

		if($dosubmit)
	    {
			if(!in_array($unit, array('y', 'm', 'd')) || !preg_match("/^[0-9]+$/", $number) || $number < 1) showmessage('参数错误！');
			$price = $r['price_'.$unit];
			if(intval($price) == 0) showmessage('价格为0，不允许续费！');
			$total = $number*$price;
			$pay_api = load('pay_api.class.php', 'pay');
			if(!$pay_api->update_exchange('member', $M['paytype'], -$total, "会员续费"))
			{
				showmessage($pay_api->error());
			}
			$group->extend_continue($_userid, $groupid, $unit, $number);
			showmessage('操作成功！', $forward);
		}
		else
	    {
			extract($r);
			$balance = $M['paytype'] == 'amount' ? $_amount : $_point;
		    $head['title'] = '会员续费_会员中心_'.$SETTING['sitename'];
			include template('upgrade_continue.html','member');
		}
		break;
	
	// 修改资料
	case 'edit':
		if(!$_userid) showmessage('您还没有登陆，请登陆',url("?module=member&action=login&forward=".urlencode(URL)));
		if($dosubmit)
		{
			
			
			// 加载会员资料更新类
			if(!class_exists('member_update'))
			{
				require CACHE_MODEL_PATH.'member_update.class.php';
			}
			$member_update = new member_update($_modelid, $_userid);
			
			/*if($SETTING['uc'])
			{
				$username = $_username;
				$email = $_email;
				$password = $_password;
				$action = 'resetpassowrd';
				require MOD_ROOT.'api/passport_server_ucenter.php';
			}*/
			$inputinfo = $member_input->get($info);
			if(isset($info) && is_array($info))
			{	
				if(!$member->edit($info)) showmessage($member->msg());
			}
			
			$modelinfo = $inputinfo['model'];
			if($modelinfo)
			{
				$modelinfo['userid'] = $_userid;
				$member_update->update($modelinfo);
				$member->edit_model($_modelid, $modelinfo);
			}
			showmessage($LANG['operation_success'], $forward);
		}
		else
		{
			$memberinfo = $member->get($_userid, $fields = '*', 1);
			$memberinfo['avatar'] = avatar($_userid);
			@extract(new_htmlspecialchars($memberinfo));
			$data = $member->get_model_info($_userid, $_modelid);
			$forminfos = $member_form->get($data);
		
			$head['title'] = $LANG['member_profile_edit'];
			$head['keywords'] = $LANG['member_profile_edit'];
			$head['description'] = $LANG['member_profile_edit'];
			
			include template('edit.html','member');
		}
		break;
	
	// 修改密码
	case 'resetpassowrd':
		if(!$_userid) showmessage('您还没有登陆，请登陆',url("?module=member&action=login&forward=".urlencode(URL)));
		if($dosubmit)
		{
			$result = $member->set_password($old_password, $new_password);
			if(!$result) showmessage($member->msg());
			/*if($SETTING['uc'])
			{
				$username = $_username;
				$email = $_email;
				$action = 'resetpassowrd';
				require MOD_ROOT.'api/passport_server_ucenter.php';
			}*/
			$member->login($username, $new_password);
			showmessage($LANG['operation_success'], $forward);
		}
		else
		{
			$head['title'] = $LANG['member_profile_edit'];
			$head['keywords'] = $LANG['member_profile_edit'];
			$head['description'] = $LANG['member_profile_edit'];
			$memberinfo = $member->get($_userid, $fields = '*', 1);
			$memberinfo['avatar'] = avatar($_userid);
			extract($memberinfo); 
			include template('resetpassowrd.html','member');
		}
		break;
	
	// 忘记密码
	case 'getpassword':
		// 收到邮件重设密码
		if($authstr){
			if($dosubmit)
			{
				if(!$member->match_authcode($userid, $authstr)) showmessage($LANG['verify_string_not_correct']);
				if(!$member->edit_password($userid, $password))
				{
					showmessage($member->msg(), $forward);
				}
				/*if($SETTING['uc'])
				{
					$action = 'resetpassowrd';
					require MOD_ROOT.'api/passport_server_ucenter.php';
				}*/
				showmessage('修改成功', SITE_URL);
			}
			else{
				
				if(!$member->match_authcode($userid, $authstr)) showmessage('请获取正确的验证码');
				include template('getpassword.html','member');
			}
		}
		else{
			if($dosubmit)
			{
				//$userid = $member->match_user_email($email,$username);
				//if(!$userid) showmessage("用户或邮箱填写错误", $forward);
				
				if(!is_email($email)){
					showmessage("邮箱格式不正确", $forward);
				}
				else{
					$sql = "SELECT * FROM $member->table WHERE email='$email' LIMIT 1";
					$r = $db->get_one($sql);
				}
				
				$userid = $r['userid'];
				$email = $r['email'];
				$memberinfo = array('email'=>$email);
				$authstr = $member->make_authcode($memberinfo);
				$member->match_authcode(80,$authstr);
				$title = $SETTING['sitename'].$LANG['get_password'];
				$authurl = SITE_URL."?module=member&action=getpassword&userid=$userid&authstr=$authstr";
				$content = tpl_data($module, 'getpassword_mailcheck.html');
				
				$sendmail = load('sendmail.class.php');
				$sendmail->send($email,$title, stripslashes($content), $SETTING['mail_user']);
				showmessage($LANG['please_login_your_mailbox'], SITE_URL);
			}
			else{
				include template('getpassword.html','member');
			}
		}
	break;
	
	// 邮箱验证
	case 'mail':
		if($dosubmit)
		{
			if(!$member->is_username($username)) showmessage($member->msg());
			if(!is_email($email)) showmessage($LANG['input_valid_email']);
			$userid = $member->match_user_email($username, $email);
			if(!$userid) showmessage($member->msg());
			if(!class_exists('sendmail'))
			{
				$sendmail = load('sendmail.class.php');
			}
			$memberinfo = array('username'=>$username);
			$authstr = $member->make_authcode($memberinfo);
			$title = $SETTING['sitename'].$LANG['member_register_email_verify'];
			$authurl = SITE_URL.$M['url']."register.php?action=activate&userid=$userid&authcode=$authstr";
			$content = tpl_data($module, 'register_mailcheck.html');
			$sendmail->send($email, $title, stripslashes($content), $SETTING['mail_user']);
			showmessage($LANG['profile_post_success'], $forward);
		}
		else 
		{
			include template('email.html','member');
		}
		break;

	
	// 会员列表
	case 'list':
		$modelid = $modelid ?  intval($modelid) :  10;
		$member_model = $member->MODEL;
		if($modelid < 1 || !in_array($modelid, array_keys($member_model))) showmessage('选择正确的模型');
		if(!$modelid) showmessage('请选择模型');
		include template('list.html','member');
	break;
	
	// 会员详细,查看会员
	case 'view':
		if(!$forward) $forward = HTTP_REFERER;
		$userid = isset($userid) ? $userid : $_userid;
		if($userid < 1) return false;
		$is_host = ($userid == $_userid) ? 1 : 0;
		$memberinfo = $member->get($userid, '*', 1);
		if($memberinfo['disabled']) showmessage('该用户已被禁用', $forward);
		if(!$memberinfo) showmessage('请选择有效用户', $forward);
		if(!class_exists('member_output'))
		{
			require CACHE_MODEL_PATH.'member_output.class.php';
		}
		
		@extract(new_htmlspecialchars($memberinfo));
		$data = $member->get_model_info($userid, $modelid);
		$member_output = new member_output($modelid, $userid);
		$forminfos = $member_output->get($data);
		if($userid != $_userid)
		{
			$models = cache_read($modelid.'_fields.inc.php', CACHE_MODEL_PATH);
			foreach($forminfos as $f)
			{
				if($models[$f]['isshow']!=1) unset($forminfos[$f]);
			}
		}
		$avatar = avatar($userid);
		if($regtime) $regtime = date('Y-m-d H:m:i', $regtime);
		if($lastlogintime) $lastlogintime = date('Y-m-d H:m:i', $lastlogintime);
		
		unset($memberinfo);
		include template('view.html','member');
	break;
	
	case 'get_remaintime':
		require MOD_ROOT.'group.class.php';
		$group = new group();
		$array = $group->extend_get($_userid, $groupid, 'startdate, enddate');
		$data = $array['startdate'].'~'.$array['enddate'];
		exit($data);
	break;
	
	case 'editanwser':
		if($dosubmit)
		{
			if(!$member->edit_answer($info['question'], $info['answer'], $info['password']))
			{
				showmessage($member->msg(), $forward);
			}
			showmessage('操作成功', $forward);
		}
		else
		{
			$avatar = avatar($_userid);
			include template('editanswer.html','member');
		}
	break;
	
	// 用户搜索
	case 'search':
		if((isset($G) && !$G['allowsearch']) || (!isset($G) && !$GROUP['3']['allowsearch'])) showmessage('你所在的用户组不允许搜索');
		foreach($MODEL as $model)
		{
			if($model['modeltype'] == 2)
			{
				$submenu[] = $model;
			}
		}
		
		require CACHE_MODEL_PATH.'member_search_form.class.php';
		$modelname = $MODEL[$modelid]['name'];
		$form = new member_search_form($modelid);
		$where = $form->get_where();
		$order = $form->get_order();
		if(!class_exists('member_search'))
		{
			require	CACHE_MODEL_PATH.'member_search.class.php';
		}
		
		if($modelid)
		{
			$search = new member_search($modelid);
			
			$data = $search->data($page,20);
			$pages = $search->pages;
			$total = $search->total;
		}
		else
		{
			$condition = '';
			$condition .= $username ? " AND username like '%$username%'" : '';
			$condition .= $groupid ? " AND groupid='$groupid'" : '';
			$condition .= $email ? " AND email='$email'" : '';
			$condition .= $frommoney ? " and amount>='$frommoney'" : '';
			$condition .= $tomoney ? " AND amount<='$tomoney'" : '';
			$condition .= $frompoint ? " AND point>='$frompoint'" : '';
			$condition .= $topoint ? " AND point<='$topoint'" : '';
			$condition .= $fromcredit ? " AND credit>='$fromcredit'" : '';
			$condition .= $tocredit ? " AND credit<='$tocredit'" : '';
			$condition .= $modelid ? " AND modelid='$modelid'" : '';
			$condition .= $areaid ? " AND areaid='$areaid'" : '';
			$condition .= (isset($disabled) && ($disabled != -1) && !empty($disabled)) ? " AND disabled=$disabled" : '';
			$order = 'userid ASC';
			$pagesize = 10;
			$data = $member->listinfo($condition, $order, $page, $pagesize, 1);
			if($data) $total = count($data);
		}
		include template('search_index.html','member');
	break;
	
	// 用户注册文件
	case 'reagreement':
		require './includes/common.inc.php';
		$license = format_textarea($M['reglicense']);
		include template($module.'_regagreement.php');
	break;
	
	
	// 用户登出
	case 'logout':
		/*if($SETTING['enableserverpassport'])
		{
			$verify = substr(md5($SETTING['passport_serverkey']),0,8);
			$logouturl = $SETTING['passport_serverurl'].$SETTING['passport_logouturl'];
			$addstr = QUERY_STRING ? QUERY_STRING : '';
			$logouturl .= (strpos($logouturl, '?') ? '&' : '?').$addstr.'&verify='.$verify;
			header('location:'.$logouturl);
			exit;
		}*/ 
		
		$member->logout(); 
		  
		showmessage($LANG['logout_success'], url("?module=$module&action=login"));
	break;
	
	
	/*
	  个性域名
	*/
	case 'domain':
	  	if(!$_userid) showmessage('您还没有登陆，请登陆',url("?module=member&action=login&forward=".urlencode(URL)));
		if($dosubmit){
			$userid = intval($_userid);
			$prefix = strip_tags($domain);
			
			if(in_array($prefix, array('blog','profile'))) 
			{
				showmessage('此个性域名受保护，不允许注册！',$forward);
			}
			if(!$member->is_username($prefix)) 
			{
				showmessage('个性域名不符合规范！',$forward);
			}
			else if($result = $db->get_one("SELECT `prefix` FROM $member->table WHERE prefix='$prefix'"))
			{
				showmessage('个性域名已经存在！',$forward);
			}
			else
			{
				$sql = "UPDATE `$member->table` SET `prefix` = '$prefix' WHERE userid=$userid";
				if($db->query($sql)){
					showmessage('修改成功！', $forward);
				}
				else{
					showmessage('修改失败！', $forward);
				}
			}		
		}
		else{
			$result = $db->get_one("SELECT `prefix` FROM $member->table WHERE userid = $_userid");
			$domain = $result['prefix'];
			include template('domain.html','member');
		}
	break;
	
	 
	/*进行个性域名检查，用于注册时做用户体验*/
	case 'checkdomain':
	
			if(in_array($value, array('blog','profile'))) 
			{
				exit('此个性域名受保护，不允许注册！');
			}
			else if(!$member->is_username($value))
			{
				exit('个性域名不符合规范！');
			}
			else if($result = $db->get_one("SELECT 'prefix' FROM $member->table WHERE prefix='$value'")){
				exit('个性域名已经存在！');
			}
			else
			{
				exit('success');
			}
		break;
	
	/*
		登录名修改
	*/
	case 'username':
		if(!$_userid) showmessage('您还没有登陆，请登陆',url("?module=member&action=login&forward=".urlencode(URL)));
		if($dosubmit){
			$userid = intval($_userid);
			if($userid < 1 || $userid == 1 || $username == 'admin') showmessage('被限制修改！');
			if(!$member->is_username($username)) showmessage('用户名不符合规范！');
			
			if($member->username_exists($username))
			{
				showmessage('用户名已经存在！');
			}
			else
			{
				$sql = "UPDATE `$member->table` SET username = '$username' WHERE userid=$userid";
				if($db->query($sql)){
					showmessage('修改成功！', url("?module=member"));
				}
				else{
					showmessage('修改失败！', url("?module=member"));
				}
			}		
		}
		else{
			include template('username.html','member');
		}
	break;
	
	/*发现*/
	case 'explore':
		include template('explore.html','member');
	break;
	
	/*男会员*/
	case 'female':
		include template('female.html','member');
	break;
	
	/*女会员*/
	case 'male':
		include template('male.html','member');
	break;
	
	/*不男不女*/
	case 'ladyboy':
		include template('ladyboy.html','member');
	break;
	
	/*我的访客*/
	case 'visitor':
		include MOD_ROOT.'member/includes/visitor.class.php';
		$visitor = new visitor();
		$visitors = $visitor->listinfo(" userid = $_userid",$page,20);
		$pages = $visitor->pages;
		include template('visitor.html','member');
	break;
	
	/*我的足迹*/
	case 'trace':
		include MOD_ROOT.'member/includes/visitor.class.php';
		$visitor = new visitor();
		$traces = $visitor->listinfo(" visitorid = $_userid",$page,20);
		$pages = $visitor->pages;
		include template('trace.html','member');
	break;
	
	/*我的黑名单*/
	case 'blacklist':
	
	break;
	
	/*我的粉丝*/
	case 'fans':
	
	break;
	
	/*我的偶像*/
	case 'follow':
	
	break;
	
	/*时间轴*/
	case 'timeline':
	
	break;
	
	default:
		$memberinfo = $member->get($_userid, $fields = '*', 1);
		$memberinfo['avatar'] = avatar($_userid);
		@extract(new_htmlspecialchars($memberinfo));
		
		if(isset($MODULE['message']))
		{
			$message_api = load('message_api.class.php', 'message', 'api');
			$msg_num = $message_api->count_message($_userid, 'new');
			if($_userid && $msg_num < 1)
			{
				$arr_msg = array('userid'=>$_userid, 'message'=>0);
				$member->edit($arr_msg);
			}
		}
		include template('index.html','member');
	break;
}
?>